package cn.mapper;

import cn.entity.SysArea;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

public class SysAreaSqlProvider {
    public String selectAreas(HashMap<String,Object> map){
        return new SQL(){{
            SELECT("sa.*,saa.name parent_name ");
            FROM(" sys_area sa ");
            LEFT_OUTER_JOIN(" sys_area saa on sa.parent_id=saa.id ");
            WHERE("sa.del_flag=0 and sa.id !=1");
            if (map.containsKey("name")&& !StringUtils.isEmpty(map.get("name"))){
                WHERE("sa.name like concat('%',#{name},'%') ");
            }else if (map.containsKey("aid")&& !StringUtils.isEmpty(map.get("aid"))){
                WHERE("sa.id = #{aid} ");
            }
        }}.toString();
    }

    public String selectChangeTree(Long id){
        return new SQL(){{
            SELECT("sa.*,saa.name parent_name ");
            FROM(" sys_area sa ");
            LEFT_OUTER_JOIN(" sys_area saa on sa.parent_id=saa.id ");
            WHERE("sa.del_flag=0 and sa.parent_ids not like concat('%,',#{id},',%')");
        }}.toString();
    }

    public String insertBatch(@Param("areas") List<SysArea> areas){
        return new SQL(){{
            INSERT_INTO("sys_area ");
            INTO_COLUMNS("  `parent_id`, `parent_ids`, `code`, `name`, `type`, `create_by`, `create_date`, `update_by`, `update_date`, `remarks`, `del_flag`, `icon` ");
            for (int i = 0; i < areas.size(); i++) {
                INTO_VALUES("#{areas["+i+"].parentId},#{areas["+i+"].parentIds}, #{areas["+i+"].code}, #{areas["+i+"].name}, #{areas["+i+"].type}, #{areas["+i+"].createBy}, #{areas["+i+"].createDate}, #{areas["+i+"].updateBy}, #{areas["+i+"].updateDate}, #{areas["+i+"].remarks}, #{areas["+i+"].delFlag}, #{areas["+i+"].icon} ");
                ADD_ROW();
            }
        }}.toString();
    }

}
